<?php require VIEWPATH . ADMIN_DIR . DIRECTORY_SEPARATOR . 'header.php' ?>
<div class="page-header">
	<h1 style="font-size: 20px;">
		<a class="a-back" href="<?= admin_url('contents/result_grade_data/') . $majorId ?>">
            <i class="fa fa-reply"></i>
        </a>
		<?= $majorTitle ?> &nbsp; <?= $gradeName ?>
		<!-- <small>
			<a href="<?= admin_url('contents/edit_result_page') . '?result_grade_id=' . $resultGradeId ?>" >
				<i class="fa fa-plus"></i> 添加学生成绩
			</a>
		</small> -->
		<a href="<?= admin_url('contents/edit_result_page') . '?result_grade_id=' . $resultGradeId ?>" class="btn btn-info a-custom">
			<i class="ace-icon fa fa-plus"></i> 添加学生成绩
		</a>
	</h1>
</div>
<div class="col-xs-12 content-filter">
	<div class="widget-box">
		<div class="widget-header" style="vertical-align:middle;">
			<h4 class="widget-title">
				内容筛选
			</h4>
			<div class="widget-toolbar">
				<a href="#" data-action="collapse">
					<i class="ace-icon fa fa-chevron-up"></i>
				</a>
				<a href="#" data-action="close">
					<i class="ace-icon fa fa-times"></i>
				</a>
			</div>
		</div>
		<div class="widget-body">
			<div class="widget-main">
				<div class="form-inline keyword-search-div">
					<label class="keyword-search-label">名称</label>
					<div class="keyword-search-box">
						<div class="div-search">
							<input style="float:left;height:30px;width:220px;border:1px solid #669FC7;padding:0 30px 0 14px;border-radius:10px 0 0 10px !important;-moz-border-radius:10px 0 0 10px;" type="text" name="realname" placeholder="搜索学生名称..." autocomplete="off">
							<span id="empty-search" style="position:relative;right:64px;cursor:pointer;display:none;">×</span>
							<button id="btn-search" class="btn-search" style="outline:none;float:left;height:30px;width:44px;border:1px solid #669FC7;background-color:#669FC7;border:0;border-radius:0 10px 10px 0;-moz-border-radius:0 10px 10px 0;">
								<i class="ace-icon fa fa-search nav-search-icon white"></i>
							</button>
						</div>
					</div>
				</div>
				<div class="form-inline keyword-search-div">
					<label class="keyword-search-label">班级</label>
					<div class="keyword-search-box div-classes">
						<a href="javascript:;" class="keyword-search-keyword keyword-search-class active" data-id="0">全部</a>
						<?php foreach($classArr as $class): ?>
							<a href="javascript:;" class="keyword-search-keyword keyword-search-class" data-id="<?=$class['id']?>"><?=$class['class_name']?></a>
						<?php endforeach; ?>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
<div class="col-xs-12">
	<!-- <a href="<?= admin_url('contents/edit_result_page') . '?result_grade_id=' . $resultGradeId ?>" class="btn btn-primary">
		<i class="ace-icon fa fa-plus"></i> 添加学生成绩
	</a> -->
	<table id="dynamic-table" class="table table-striped table-bordered table-hover" style="margin-top:10px">
		<thead>
			<tr>
				<th>学生名称</th>
				<th>所属班级</th>
				<th>座位号</th>
				<?php foreach($subjectArr as $subjectName): ?>
					<th><?= $subjectName ?></th>
				<?php endforeach ?>
				<th>总分</th>
				<th>班级排名</th>
				<th>年级排名</th>
				<th>评语</th>
				<th>操作</th>
			</tr>
		</thead>
		<tbody>
		</tbody>
	</table>
</div>

<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'footer.php'?>
<script type="text/javascript">
	var table = "";
	$(function(){
		// 考试列表
		table = $('#dynamic-table').dt({
			"ajaxUrl": '<?= admin_url('contents/get_result_list/' . $resultGradeId)?>',
			"columns": [
				{"data": function(ret){
	            	return ret.realname;
				}},
				{"data": function(ret){
	            	return ret.className;
				}},
				{"data": function(ret){
	            	return ret.seat_no;
				}},
				<?php foreach($subjectArr as $subjectId => $subjectName): ?>
				{"data": function(ret){
					return ret.value[<?= $subjectId ?>] ?  ret.value[<?= $subjectId ?>].score : 0;
				}},
				<?php endforeach ?>
				{"data": function(ret){
	            	return ret.total;
				}},
				{"data": function(ret){
	            	return ret.class_rank;
				}},
				{"data": function(ret){
	            	return ret.grade_rank;
				}},
				{"data": function(ret){
	            	return ret.remark;
				}},
				{"data": function(ret){
					var action = '';
					action += '<a href="<?= admin_url('contents/edit_result_page?result_grade_id=' . $resultGradeId . '&result_id=')?>' + ret.id + '" class="mr10 text-success"><i class="fa fa-edit"></i> 编辑</a>&nbsp;&nbsp;';
					action += '<a data-id="' + ret.id + '" data-name="' + ret.realname + '" class="delete-result text-danger" href="javascript:;"><i class="fa fa-times"></i> 删除</a>';
					return action;
				}},
			]
		});
		// 点击关键字
		$("body").on("click", ".keyword-search-keyword", function(){
			// 高亮当前点击对象
			$(this).parent().find(".keyword-search-keyword").removeClass("active");
			$(this).addClass("active");
			var id = $(this).data("id");
			var realname = $("[name=realname]").val();
			// 筛选班级
			table.setAjaxData({
				"class_id": id,
				'realname': realname
			});
		})
		// 敲回车就搜索
		$("[name=realname]").keypress(function (e) {
                if (e.which == 13) {
                    $("#btn-search").trigger("click");
                }
		});
		// 清空搜索框
		$("[name=realname]").keyup(function() {
			if($(this).val().length > 0) {
				$("#empty-search").show();
				$("#empty-search").click(function(){
					$("[name=realname]").val('');
					$(this).hide();
					// 重置搜索结果
					var classId = $(".div-classes > .active").data("id");
					table.setAjaxData({
						"class_id": classId
					});
				})
			} else {
				$("#empty-search").hide();
			}
		}); 
		// 搜索名称
		$("#btn-search").click(function() {
			var realname = $("[name=realname]").val();
			var classId = $(".div-classes > .active").data("id");
			if(realname != '') {
				table.setAjaxData({
					"class_id": classId,
					'realname': realname
				})
			} else {
				table.setAjaxData({
					"class_id": classId,
				})
			}
		});
		// 删除某场考试
		$("body").on("click", ".delete-result", function(){
			var id = $(this).data("id");
			var name = $(this).data("name");
			layer.confirm('确定删除：' + name + ' 成绩吗？', function(){
				// 提交数据
				var postUrl = "<?=admin_url('contents/delete_result_action')?>";
				var postData = {"result_id": id};
				ajax_post(postUrl, postData, function(res){
					table.draw(false);
				})
			});
		})
	})
</script>